package cn.edu.sdjzu.xg11.dao;

import java.util.Collection;
import java.util.TreeSet;

import org.springframework.stereotype.Repository;

import cn.edu.sdjzu.xg11.domain.Degree;
@Repository
public final class DegreeDao {
	private static Collection<Degree> degrees;
	static{
		degrees = new TreeSet<Degree>();
		Degree doctor = new Degree(1,"博士","01","");
		degrees.add(doctor);
		degrees.add(new Degree(2,"硕士","02",""));
		degrees.add(new Degree(3,"学士","03",""));
	}
	public Collection<Degree> getDegrees(){
		return DegreeDao.degrees;
	}
	
	public Degree getDegree(Integer id){
		Degree desiredDegree = null;
		for (Degree degree : degrees) {
			if(id.equals(degree.getId())){
				desiredDegree =  degree; 
				break;
			}
		}
		return desiredDegree;
	}
	
	public boolean updateDegree(Degree degree){
		degrees.remove(degree);
		return degrees.add(degree);		
	}
	
	public boolean addDegree(Degree degree){
		return degrees.add(degree);		
	}

	public boolean deleteDegree(Integer id){
		Degree degree = this.getDegree(id);
		return this.deleteDegree(degree);
	}
	
	public boolean deleteDegree(Degree degree){
		return degrees.remove(degree);
	}
	
	
	public static void main(String[] args){
		DegreeDao dao = new DegreeDao();
		Collection<Degree> degrees = dao.getDegrees();
		display(degrees);
		Degree degree = new Degree(2,"硕士","02","");
		degree.setNo("02x");
		dao.updateDegree(degree);
		display(degrees);
	
		dao.deleteDegree(degree);
		dao.deleteDegree(3);
		display(degrees);
		dao.addDegree(new Degree(2,"戰士","04",""));
		display(degrees);
		System.out.println("1="+dao.getDegree(2));
	}

	private static void display(Collection<Degree> degrees) {
		for (Degree degree : degrees) {
			System.out.println(degree);
		}
	}
	
	
}
